/*
 * @Author: Gavin
 * @Date: 2018-09-07 10:54:49
 * @LastEditors: Gavin
 * @LastEditTime: 2018-09-07 13:32:10
 * @Description:
 */

let babelConfig = require('../babel')
let MiniCssExtractPlugin = require('mini-css-extract-plugin')

// const assets = {
//     test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
//     loader: 'url-loader?limit=8192&name=images/[hash].[name].[ext]'
// };

const eslint = {
    enforce: 'pre',
    test: /\.(js|jsx)$/i,
    include: /src/,
    exclude: /(node_modules|bower_components)/,
    loader: 'eslint-loader'
}

const jsx = (ispod, target) => {
    return {
        test: /\.(js|jsx)$/,
        include: /src/,
        // exclude: /(node_modules|bower_components)/,
        exclude: [/node_modules(?![\/\\]preact-mdc)/],
        use: [{
            loader: "babel-loader",
            options: babelConfig(ispod, target)
        }]
    }
}

const img = {
    test: /\.(jpe?g|png|gif)$/,
    use: [{
        loader: 'url-loader',
        options: {
            limit: 8192,    // 小于8k的图片自动转成base64格式，并且不会存在实体图片
            outputPath: 'images/'   // 图片打包后存放的目录
        }
    }
    ]
}
const file = {
    test: /\.(eot|ttf|woff?2)$/,
    use: 'file-loader'
}
const svg = {
    test: /\.(svg)$/i,
    loader: 'svg-sprite-loader',
    include: [
        require.resolve('antd-mobile').replace(/warn\.js$/, ''),
    ]
}

const styleLoader = { loader: 'style-loader', options: { sourceMap: true } }
const miniLoader = { loader: MiniCssExtractPlugin.loader }

const postcssLoader = { loader: 'postcss-loader', options: require('./postcss.config') }

const cssLoader = {
    loader: 'css-loader', options: {
        importLoaders: 1,
        // modules: true,
        // sourceMap: true,
        // namedExport: true,
        // camelCase: true,
        minimize: true,
        localIdentName: '[name]__[local]___[hash:base64:5]',
    }
}

const lessLoader = {
    loader: 'less-loader', options: {
        sourceMap: true,
        javascriptEnabled: true,
        strictMath: true,
        modifyVars: {
            'primary-color': '#531dab'
        }
    }
}
const css = (ispod, target) => {
    return {
        test: /\.css$/,
        // exclude: /(node_modules|bower_components)/,
        // use: [miniLoader, cssLoader, postcssLoader]
        use: [(ispod) ? miniLoader : styleLoader, cssLoader, postcssLoader]
    }
}
const less = (ispod, target) => {
    return {
        test: /\.less$/,
        // exclude: /(node_modules|bower_components)/,
        use: [(ispod) ? miniLoader : styleLoader, cssLoader, postcssLoader, lessLoader]
    }
}
const sass = (ispod, target) => {
    return {
        test: /\.(sass|scss)$/,
        // exclude: /(node_modules|bower_components)/,
        use: [(ispod) ? miniLoader : styleLoader, cssLoader, postcssLoader, { loader: 'sass-loader' }]
    }
}
module.exports = (ispod, target) => {
    return [
        jsx(ispod, target),
        svg,
        css(ispod, target),
        less(ispod, target),
        img,
        file
    ]
}
